﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace JavaCodeGenerator
{
    public partial class Form1 : Form
    {

        private string modelName = string.Empty;
        private string referModelName = string.Empty;
        private string referPackageName = string.Empty;


        // 新实体包路径
        private string modelPkgPath = string.Empty;
        private string mapperPkgPath = string.Empty;
        private string daoPkgPath = string.Empty;
        private string daoImpPkgPath = string.Empty;
        private string servicePkgPath = string.Empty;
        private string serviceImpPkgPath = string.Empty;

        // 参照的文件路径
        private string daoPkgPathRefer = string.Empty;
        private string daoImpPkgPathRefer = string.Empty;
        private string servicePkgPathRefer = string.Empty;
        private string serviceImpPkgPathRefer = string.Empty;

        // 文件后缀
        private const string extensions = ".java";

        Config config = ConfigUtil.getConfig();

        public Form1()
        {
            InitializeComponent();
            this.InitData();
        }

        #region 事件

        private void BtnSelectPath_Click(object sender, EventArgs e)
        {

            Button btn = (Button)sender;
            FolderBrowserDialog dialog = new FolderBrowserDialog();

            if (dialog.ShowDialog() == DialogResult.OK)
            {
                switch (btn.Name)
                {

                    case "btnSelectModelPackagePath":
                        this.txtModelPackagePath.Text = dialog.SelectedPath;
                        break;

                    case "btnSelectDaoPackagePath":
                        this.txtDaoPackagePath.Text = dialog.SelectedPath;
                        break;

                    case "btnSelectDaoImpPackagePath":
                        this.txtDaoImpPackagePath.Text = dialog.SelectedPath;
                        break;

                    case "btnSelectServicePackagePath":
                        this.txtServicePackagePath.Text = dialog.SelectedPath;
                        break;

                    case "btnSelectServiceImpPackagePath":
                        this.txtServiceImpPackagePath.Text = dialog.SelectedPath;
                        break;

                    case "btnSelectMapperPackagePath":
                        this.txtMapperPackagePath.Text = dialog.SelectedPath;
                        break;
                }
            }
        }

        private void BtnGenerate_Click(object sender, EventArgs e)
        {

            if (!CheckSelectPath())
                return;

            if (!CheckReferPath())
                return;

            // 创建包
            String packagename = this.txtPackageName.Text.Trim().ToLower();
            string path = modelPkgPath + "\\" + packagename;
            if (!Directory.Exists(path))
                Directory.CreateDirectory(path);

            path = mapperPkgPath + "\\" + packagename;
            if (!Directory.Exists(path))
                Directory.CreateDirectory(path);

            path = daoPkgPath + "\\" + packagename;
            if (!Directory.Exists(path))
                Directory.CreateDirectory(path);

            path = daoImpPkgPath + "\\" + packagename;
            if (!Directory.Exists(path))
                Directory.CreateDirectory(path);

            path = servicePkgPath + "\\" + packagename;
            if (!Directory.Exists(path))
                Directory.CreateDirectory(path);

            path = serviceImpPkgPath + "\\" + packagename;
            if (!Directory.Exists(path))
                Directory.CreateDirectory(path);


            // 生成新的文件
            String newDaoFile = daoPkgPath + "\\" + packagename + "\\" + modelName + config.DaoFileSuffix + extensions;
            File.Copy(daoPkgPathRefer, newDaoFile, true);

            String newDaoImpFile = daoImpPkgPath + "\\" + packagename + "\\" + modelName + config.DaoImplFileSuffix + extensions;
            File.Copy(daoImpPkgPathRefer, newDaoImpFile, true);

            String newServiceFile = servicePkgPath + "\\" + packagename + "\\" + modelName + config.ServiceFileSuffix + extensions;
            File.Copy(servicePkgPathRefer, newServiceFile, true);

            String newServiceImpFile = serviceImpPkgPath + "\\" + packagename + "\\" + modelName + config.ServiceImplFileSuffix + extensions;
            File.Copy(serviceImpPkgPathRefer, newServiceImpFile, true);


            // 替换Dao文件内容
            String fileContent = TextUtil.ReadString(newDaoFile);
            fileContent = fileContent.Replace(referModelName.ToLower(), packagename).Replace(referModelName, modelName);
            TextUtil.WriteString(newDaoFile, fileContent);

            // 替换DaoImp文件内容
            fileContent = TextUtil.ReadString(newDaoImpFile);
            fileContent = fileContent.Replace(referModelName.ToLower(), packagename).Replace(referModelName, modelName);
            TextUtil.WriteString(newDaoImpFile, fileContent);

            // 替换Service文件内容
            fileContent = TextUtil.ReadString(newServiceFile);
            fileContent = fileContent.Replace(referModelName.ToLower(), packagename).Replace(referModelName, modelName);
            TextUtil.WriteString(newServiceFile, fileContent);

            // 替换ServiceImpl文件内容
            fileContent = TextUtil.ReadString(newServiceImpFile);
            fileContent = fileContent.Replace(referModelName.ToLower(), packagename).Replace(referModelName, modelName);
            TextUtil.WriteString(newServiceImpFile, fileContent);


            ConfigUtil.setConfig(config);

            MessageBox.Show("生成成功");
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            if (!CheckSelectPath())
                return;

            // 删除包
            string path = mapperPkgPath + "\\" + this.txtModelName.Text.Trim().ToLower();
            if (Directory.Exists(path))
                Directory.Delete(path, true);

            path = daoPkgPath + "\\" + this.txtModelName.Text.Trim().ToLower();
            if (Directory.Exists(path))
                Directory.Delete(path, true);

            path = daoImpPkgPath + "\\" + this.txtModelName.Text.Trim().ToLower();
            if (Directory.Exists(path))
                Directory.Delete(path, true);

            path = servicePkgPath + "\\" + this.txtModelName.Text.Trim().ToLower();
            if (Directory.Exists(path))
                Directory.Delete(path, true);

            path = serviceImpPkgPath + "\\" + this.txtModelName.Text.Trim().ToLower();
            if (Directory.Exists(path))
                Directory.Delete(path, true);

            MessageBox.Show("撤消成功");
        }

        private void txtModelName_KeyUp(object sender, KeyEventArgs e)
        {
            this.txtPackageName.Text = this.txtModelName.Text.ToLower();
        }

        private void txtReferModelName_KeyUp(object sender, KeyEventArgs e)
        {
            this.txtReferPackageName.Text = this.txtReferModelName.Text.ToLower();
        }

        #endregion

        #region 私有方法

        private void InitData()
        {
            this.txtModelPackagePath.Text = config.ModelPackagePath;
            this.txtMapperPackagePath.Text = config.MapperPackagePath;
            this.txtDaoPackagePath.Text = config.DaoPackagePath;
            this.txtDaoImpPackagePath.Text = config.DaoImplPackagePath;
            this.txtServicePackagePath.Text = config.ServicePackagePath;
            this.txtServiceImpPackagePath.Text = config.ServiceImplPackagePath;

            this.txtModelName.Focus();
        }

        private bool CheckSelectPath()
        {
            modelName = this.txtModelName.Text.Trim();
            referModelName = this.txtReferModelName.Text.Trim();
            referPackageName = this.txtReferPackageName.Text.Trim();

            modelPkgPath = this.txtModelPackagePath.Text.Trim();
            mapperPkgPath = this.txtMapperPackagePath.Text.Trim();
            daoPkgPath = this.txtDaoPackagePath.Text.Trim();
            daoImpPkgPath = this.txtDaoImpPackagePath.Text.Trim();
            servicePkgPath = this.txtServicePackagePath.Text.Trim();
            serviceImpPkgPath = this.txtServiceImpPackagePath.Text.Trim();

            if (String.IsNullOrEmpty(modelName))
            {
                MessageBox.Show("请输入新实体名称");
                return false;
            }
            else if (String.IsNullOrEmpty(referModelName))
            {
                MessageBox.Show("请输入参照实体名称");
                return false;
            }
            else if (String.IsNullOrEmpty(modelPkgPath) || !Directory.Exists(modelPkgPath))
            {
                MessageBox.Show(modelPkgPath + "路径不存在");
                return false;
            }
            else if (String.IsNullOrEmpty(mapperPkgPath) || !Directory.Exists(mapperPkgPath))
            {
                MessageBox.Show(mapperPkgPath + "路径不存在");
                return false;
            }
            else if (String.IsNullOrEmpty(daoPkgPath) || !Directory.Exists(daoPkgPath))
            {
                MessageBox.Show(daoPkgPath + "路径不存在");
                return false;
            }
            else if (String.IsNullOrEmpty(daoImpPkgPath) || !Directory.Exists(daoImpPkgPath))
            {
                MessageBox.Show(daoImpPkgPath + "路径不存在");
                return false;
            }
            else if (String.IsNullOrEmpty(servicePkgPath) || !Directory.Exists(servicePkgPath))
            {
                MessageBox.Show(servicePkgPath + "路径不存在");
                return false;
            }
            else if (String.IsNullOrEmpty(serviceImpPkgPath) || !Directory.Exists(serviceImpPkgPath))
            {
                MessageBox.Show(serviceImpPkgPath + "路径不存在");
                return false;
            }

            config.ModelPackagePath = modelPkgPath;
            config.MapperPackagePath = mapperPkgPath;
            config.DaoPackagePath = daoPkgPath;
            config.DaoImplPackagePath = daoImpPkgPath;
            config.ServicePackagePath = servicePkgPath;
            config.ServiceImplPackagePath = serviceImpPkgPath;
            config.referModelName = referModelName;
            config.referModelPackage = referPackageName;

            return true;
        }


        private bool CheckReferPath()
        {
            string referModelName = this.txtReferModelName.Text.Trim();

            daoPkgPathRefer = this.txtDaoPackagePath.Text.Trim() + "\\" + referPackageName.ToLower() + "\\" + referModelName + config.DaoFileSuffix + extensions;
            daoImpPkgPathRefer = this.txtDaoImpPackagePath.Text.Trim() + "\\" + referPackageName.ToLower() + "\\" + referModelName + config.DaoImplFileSuffix + extensions;
            servicePkgPathRefer = this.txtServicePackagePath.Text.Trim() + "\\" + referPackageName.ToLower() + "\\" + referModelName + config.ServiceFileSuffix + extensions;
            serviceImpPkgPathRefer = this.txtServiceImpPackagePath.Text.Trim() + "\\" + referPackageName.ToLower() + "\\" + referModelName + config.ServiceImplFileSuffix + extensions;

            if (String.IsNullOrEmpty(referModelName))
            {
                MessageBox.Show("请输入参照实体名称");
                return false;
            }
            else if (String.IsNullOrEmpty(daoPkgPathRefer) || !File.Exists(daoPkgPathRefer))
            {
                MessageBox.Show(daoPkgPathRefer + "路径不存在");
                return false;
            }
            else if (String.IsNullOrEmpty(daoImpPkgPathRefer) || !File.Exists(daoImpPkgPathRefer))
            {
                MessageBox.Show(daoImpPkgPathRefer + "路径不存在");
                return false;
            }
            else if (String.IsNullOrEmpty(servicePkgPathRefer) || !File.Exists(servicePkgPathRefer))
            {
                MessageBox.Show(servicePkgPathRefer + "路径不存在");
                return false;
            }
            else if (String.IsNullOrEmpty(serviceImpPkgPathRefer) || !File.Exists(serviceImpPkgPathRefer))
            {
                MessageBox.Show(serviceImpPkgPathRefer + "路径不存在");
                return false;
            }
            return true;
        }



        #endregion

        private string projectPath = null;
        private string oldProjectName = null;
        private string newProjectName = null;


        private void btnRename_Click(object sender, EventArgs e)
        {
            projectPath = this.txtProjectPath.Text.Trim();
            oldProjectName = this.txtOldProjectName.Text.Trim();
            newProjectName = this.txtNewProjectName.Text.Trim();

            if (String.IsNullOrEmpty(projectPath) || !Directory.Exists(projectPath))
            {
                MessageBox.Show("请填写正确的项目路径");
                return;
            }
            if (String.IsNullOrEmpty(oldProjectName))
            {
                MessageBox.Show("请填写原项目名称");
                return;
            }
            if (String.IsNullOrEmpty(newProjectName))
            {
                MessageBox.Show("请填写新项目名称");
                return;
            }

            DirectoryInfo root = new DirectoryInfo(projectPath);
            rename(root);

            MessageBox.Show("操作成功");
        }

        private void rename(DirectoryInfo root)
        {
            try
            {

                // 处理当前目录下的文件
                FileInfo[] files = root.GetFiles();
                if (files != null && files.Length > 0)
                {

                    foreach (FileInfo fi in files)
                    {
                        if (fi.Name.StartsWith(".") || fi.Name.EndsWith(".class")||fi.Name.EndsWith(".jar"))
                            continue;

                        // 修改文件内容
                        String fileContent = TextUtil.ReadString(fi.FullName);
                        fileContent = fileContent.Replace(oldProjectName, newProjectName);
                        TextUtil.WriteString(fi.FullName, fileContent);

                        // 修改文件名
                        string newFileName = this.getNewName(fi.FullName, fi.Name);

                        if (newFileName != fi.FullName)
                            fi.MoveTo(newFileName);
                    }
                }


                DirectoryInfo[] directories = root.GetDirectories();
                if (directories != null && directories.Length > 0)
                {

                    // 递归处理当前目录下的子目录
                    foreach (DirectoryInfo di in directories)
                    {
                        if (di.Name.StartsWith(".") || di.Name.ToLower().Contains("maven") || di.Name.ToLower().Contains("target") || di.Name.ToLower().Contains("class"))
                            continue;
                        rename(di);
                    }
                }

                string newDirName = this.getNewName(root.FullName, root.Name);
                if (newDirName != root.FullName)
                    root.MoveTo(newDirName);

            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private string getNewName(string fullName, string name)
        {
            string newName = fullName.Substring(0, fullName.Length - name.Length) + name.Replace(oldProjectName, newProjectName);
            return newName;

        }

    }
}
